#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Created on 2015-4-21

@author: abel
'''
import os
import time
import logging
import sys
# 加入将alive_base加入path
base_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'alive_base')
sys.path.append(base_path)
import server_base
from server_base import UFOApplication
import tornado.ioloop
from tornado import httpserver
from tornado.options import define, options
import urls_base



# if this project do not need celery, annotate this line
# celery = ufo_utils.load_feed(feed_app='start_ugc_server', feed_config=C('UGC_FEED'))

class App(UFOApplication):
    '''
    '''
    
    def __init__(self):
        
        self.use_mysql_pool = False

        self.db_names = ()
        self.mongo_db_names = ()
        UFOApplication.__init__(self, urls_base.handlers,
                                template_path=os.path.join(os.path.dirname(__file__),"template"))
        
        
        
    
        
def main():
    start_time = time.time()
    
    # handle the commmand line parameters
    input_params = tornado.options.parse_command_line()
#     if(len(input_params) > 0):
    port = options.port
 
    # set log file
    log_config_file = os.path.join(os.getcwd(), 'logging.cfg')
    server_base.initlog(log_config_file, port)

    # create http server instance
    _http_server_app = App()
    
    server_base.set_app(_http_server_app)
    # added signal callback to interrupt app
    server_base.set_signals()
    
    # startup the tata
    http_server = httpserver.HTTPServer(_http_server_app)
    http_server.listen(port)
    
    end_time = time.time()
    logging.info('...server started, %sms used...' % ((end_time - start_time) * 1000))
    
    tornado.ioloop.IOLoop.instance().start()


if __name__ == "__main__":
    define("port", default=20000, help="run on the given port", type=int)
    define("debug", default=True, help="debug mode", type=bool)
    main()
